package com.xiaobias.distributed.pan.web.file;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.xiaobias.distributed.pan.LayuiData;
import com.xiaobias.distributed.pan.dto.BaiduPanFileList;
import com.xiaobias.distributed.pan.service.BaiduPanFileService;

import java.util.List;
import javax.annotation.Resource;

import com.xiaobias.distributed.pan.web.common.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"/baidu/pan/rest/2.0/xpan/file"})
public class ListController {
    @Resource
    private BaiduPanFileService baiduPanFileService;

    @RequestMapping({"/list.html"})
    public ModelAndView filePage(ModelAndView modelAndView) {
        modelAndView.setViewName("/pan/file/list");
        return modelAndView;
    }

    @RequestMapping({"getListData"})
    @ResponseBody
    public LayuiData getListDate(String dir, @RequestParam(defaultValue = "0") String folder) {
        BaiduPanFileList fileList = this.baiduPanFileService.getFileList(dir, folder);
        List list = fileList.getList();
        return LayuiData.data(list);
    }

    @ResponseBody
    @GetMapping("/directory")
    public Result<JSONArray> list(String account, @RequestParam(defaultValue = "/") String dir) {
        JSONObject maps = baiduPanFileService.getFileListByAccount(account, dir);
        return Result.successful(maps.getJSONArray("list"));
    }
}